package com.ponxu.mb.web.action.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ponxu.log.Log;
import com.ponxu.log.LogFactory;
import com.ponxu.mb.model.Category;
import com.ponxu.mb.service.CategoryService;
import com.ponxu.mb.web.action.MiniBlogAction;
import com.ponxu.utils.Lang;
import com.ponxu.web.core.WebContext;

/**
 * 分类/标签 管理
 * 
 * @author xwz
 */
public class CategoryAction extends MiniBlogAction {
	private static final Log LOG = LogFactory.getLog();
	private static final String FTL_CATEGORY = "/admin/category.ftl";

	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);

		int id = context.getIntParameter("id");
		String listType = Lang.defaultString(context.getParameter("type"),
				"cats");

		if (id != 0) {
			context.put("obj", CategoryService.getById(id));
		}

		context.put("cats", CategoryService.searchAllCategory());
		context.put("tags", CategoryService.searchAllTag());
		context.put("type", listType);
		return FTL_CATEGORY;
	}

	/**
	 * 保存 标签/分类
	 */
	public String save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int id = context.getIntParameter("id");
		String name = context.getParameter("name");
		String seoName = context.getParameter("seoName");
		String type = context.getParameter("type");
		int parentId = context.getIntParameter("parentId");
		LOG.debug("name=%s", name);

		String listType = "tag".equals(type) ? "tags" : "cats";

		String msg = "保存成功!";
		if (Lang.isNotEmpty(name)) {
			Category cat = null;
			if (id == 0) {
				cat = new Category();
			} else {
				cat = CategoryService.getById(id);
			}

			cat.setName(name);
			cat.setSeoName(seoName);
			cat.setType(type);
			cat.setParentId(parentId);
			int i = 0;
			if (id == 0) {
				i = CategoryService.add(cat);
			} else {
				i = CategoryService.update(cat);
			}

			if (i == 0) {
				msg = "保存失败!";
			}
		} else {
			msg = "添加失败, 名称为空!";
		}

		context.put("msg", msg);
		context.forward("/admin/category?oper=&id=&type=" + listType);
		return null;
	}

	private int[] getIds(HttpServletRequest request) {
		String[] idstr = request.getParameterValues("id");
		int size = idstr.length;
		int[] ids = new int[size];
		for (int i = 0; i < size; i++) {
			ids[i] = Integer.parseInt(idstr[i]);
		}
		return ids;
	}

	/**
	 * 删除 标签/分类
	 */
	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int[] ids = getIds(request);
		int i = CategoryService.delete(ids);
		String msg = "删除成功!";

		if (i == 0) {
			msg = "删除失败!";
		}

		String listType = "tags".equals(context.getParameter("deleteType")) ? "tags"
				: "cats";

		context.put("msg", msg);
		context.forward("/admin/category?oper=&type=" + listType);
		return null;
	}
}